<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

/**
* This controller is used for configuration feature of the component
*/
class JAJobBoardControllerJaApplications extends JAJobBoardController{

    /**
     * Constructor
     */
    function __construct( $default = array() )
    {
    	$layout = JRequest::getVar('layout');
        parent::__construct( $default );
        $this->set('name', 'applications');
        $this->set('fullname', 'jaapplications');
        
        // Register Extra tasks
        $this->registerTask( 'add',        'edit' );
        $this->registerTask( 'apply',    'save' );
        $this->registerTask( 'inactivate',    'activate' );
    }
    
    /**
    * Display current configuration of the component to administrator
    * 
    */
    function display(){
		JRequest::setVar('view', $this->get('fullname')); // Force using associated view
        parent::display();
		
    }
    
    /**
    * Cancel current operation
    * 
    */
    function cancel(){
        $Itemid = JRequest::getVar('menuId');
        $this->setRedirect("index.php?option=". JBCOMNAME ."&view=". $this->get('fullname')."&menuId=".$Itemid);
    }
    
    /**
    * Remove a configuration row
    * 
    */
    function remove(){
    	// Check for request forgeries
		// JRequest::checkToken() or jexit( 'Invalid Token' );
        $task = JRequest::getVar('task', 'remove');
        $limited = JRequest::getVar('limited', 0);
        $menuId = JRequest::getVar('menuId');
        $model =& $this->getModel($this->get('fullname'));
        if($n = $model->remove($task)){
        	$this->setMessage(JText::_('Successfully removed application(s)'));
		    $this->setRedirect('index.php?option=com_jajobboard&view=jaapplications&menuId='.$menuId);
        } else {
            echo "<script>alert('".JText::_('Could not remove the application')."'); window.history.go(-1);</script>";
        }
	}
    
	
	/**
	* Save configuration record
	*/
	function save(){
        // Check for request forgeries
       	global $option;
        $cid = JRequest::getVar('cid',  0, '', 'array');
        $step = JRequest::getVar('step', ''); 
        ($step == 'done') or JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $menuId = JRequest::getVar('menuId');                 
		$task = JRequest::getVar('task');
        $model    =& $this->getModel( $this->get('fullname') );
        
        $rc = $model->store();       
        if($rc){
//            $item = $model->getItem();
            if ((isset($cid[0]))&&$cid[0]!=0)
				 $msg = JText::_( 'Successfully updated application' );
			else 
				$msg = JText::_( 'Successfully created application' );
            
//            return $item->job_id;
        }
        else 
        {
        	JRequest::setVar('app_postback',true);
			JRequest::setVar('task','edit');
			parent::display();
        	return false;
        }
        switch ( $task ) {
			case 'apply':
				$this->setRedirect( "index.php?option=$option&view=jaapplications&task=edit&cid[]=".$cid[0] , $msg );
				break;

			case 'save':
			default:
				$this->setRedirect( "index.php?option=$option&view=jaapplications", $msg );
				break;
		}
	}
    function download(){
         $model    =& $this->getModel( $this->get('fullname') );   
         $model->download();
         exit;  
    }
    
    function remove_attach()
    {
    	$model    =& $this->getModel( $this->get('fullname') );
    	$model->remove_attach();
         exit;
    }
}
?>
